//
//  EventDetailController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/26/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "EventDetailController.h"

@interface EventDetailController ()

@end

@implementation EventDetailController

@synthesize viewAdd, lblTitle, lblPrice, scrollView;

@synthesize listData, listUsers;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    listData = [[NSMutableArray alloc] init];
    listUsers = [[NSMutableArray alloc] init];
    self.title = [NSString stringWithFormat:NSLocalizedString(@"event.add.title", @"")];
    UIButton *addButton = [StyleControl makeButton:nil andFont:nil andColor:nil andImage:[UIImage imageNamed:@"whopaid"] andBackground:nil andHightlightBackground:nil];
    addButton.frame = CGRectMake(0, 0, 30, 30);
    [addButton addTarget:self action:@selector(openEventList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:addButton] autorelease];
    addButton = [StyleControl makeButton:nil andFont:nil andColor:nil andImage:nil andBackground:nil andHightlightBackground:nil];
    addButton.frame = CGRectMake(0, 0, 30, 30);
    [addButton setTitle:@"Gereed" forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(editEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:addButton] autorelease];
    [self createNavigation];
    [self loadData];
    [self changeStyle];
    [self showGripUser];
}

-(void) createNavigation
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *right =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 37, 35);
    right.frame = CGRectMake(250, 8, 60, 35);
    [right setTitle:@"Wijzig" forState:UIControlStateNormal];
    NSString *title = [NSString stringWithFormat:NSLocalizedString(@"event.edit.member", @"")];
    [left addTarget:self action:@selector(openEventList) forControlEvents:UIControlEventTouchUpInside];
    [right addTarget:self action:@selector(editMember) forControlEvents:UIControlEventTouchUpInside];
    [StyleControl naviStyle:self.view andTitle:title andColor:0 andleftButton:left andRightButton:right borderLeft:1 borderRight:1];
}
-(void) changeStyle
{
    [StyleControl background:self.view];
    [StyleControl setViewStyle:viewAdd];
    [StyleControl drawLine:self.viewAdd PositionX:0 PositionY:45 Width:306];
    [StyleControl drawLine:self.viewAdd PositionX:0 PositionY:90 Width:306];
    [StyleControl setPriceStyle:lblPrice];
}

-(void) loadData
{
    Event *event = [Event instance];
    listData = [event getDetail:event._id];
    lblTitle.text = event._name;
    double amount = 0;
    for(int i=0 ;i<[listData count];i++){
        ParticipateClass *par = [listData objectAtIndex:i];
        amount +=par.price;
    }
    lblPrice.text = [StyleControl formatCurrency:[NSString stringWithFormat:@"%.2f",amount] addCurency:YES];
}
-(void)showGripUser
{
    //scroll view
    for(UIView *subview in [self.scrollView subviews]) {
        [subview removeFromSuperview];
        //NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
    }
    //number items per row
    int a=4;
    //number items per column
    int b=2;
    if([listData count] < 7) b = 2;
    //total items + 1 for add item in last
    int c=[self.listData count];
    //width of scrollView
    int d=scrollView.frame.size.width;
    //height of scrollView
    int d2=scrollView.frame.size.height;
    //width of an item
    int e=d/a;
    //height of an item
    int f=d2/b-15;
    int i=0;
    int numPages = ceil(1.0f*c/(a*b));
    for (int p=0; p<numPages; p++) {
        for (int m=0; m<b; m++) {
            for (int n=0; n<a && i<c; n++) {
                UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(((p*d)+(n*e)), (m*f), e, f)];
                ParticipateClass *par = [listData objectAtIndex:i];
                
                UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
                im.image = [Helper getImageMember:par];
                [StyleControl setBorderStyle:im];
                [itemView addSubview:im];
                [im release];
                
                UIImageView *icon = [[UIImageView alloc] init];
                icon.frame = CGRectMake(10, 45, 15, 15);
                if(par.active == 1)
                    icon.image = [UIImage imageNamed:@"approved"];
                else{
                    icon.frame = CGRectMake(10, 35, 25, 25);
                    icon.image = [UIImage imageNamed:@"sendBlue"];
                }
                [itemView addSubview:icon];
                [icon release];
                
                UILabel *lblName = [[UILabel alloc] init];
                lblName.text = par.name;
                lblName.frame = CGRectMake(10, 60, 60, 20);
                lblName.font = [UIFont systemFontOfSize:12];
                [itemView addSubview:lblName];
                [lblName release];
                //if(par.approved == 1){
                    UILabel *lblPriceMember = [[UILabel alloc] init];
                    lblPriceMember.frame = CGRectMake(10, 75, 60, 20);
                    [StyleControl setPriceStyle:lblPriceMember Price:par.price];
                    lblPriceMember.font = [UIFont systemFontOfSize:12];
                    [itemView addSubview:lblPriceMember];
                    [lblPriceMember release];
                //}
                
                [scrollView addSubview:itemView];
                [itemView release];
                i++;
            }
        }
    }
    scrollView.contentSize = CGSizeMake(d*(numPages), d2);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
}
-(void) openEventList
{
    BookingListController *detail = [[BookingListController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}
-(void) editMember
{
    EventEditMemberController *detail = [[EventEditMemberController alloc] init];
    [self presentViewController:detail animated:YES completion:NULL];
    [detail release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [super dealloc];
}
- (void)viewDidUnload {
    [super viewDidUnload];
}
@end
